package com.itheima.optional;

/**
 * @program: itheima-jdk8
 * @description:
 * @author: zhanghz001
 * @create: 2021-01-09 14:21
 **/

import com.itheima.lambda.Student;

import java.util.Optional;

public class OptionalDemo {
    public static void main(String[] args) {
        // Optional<Integer> integer = Optional.of(123);
        // integer.ifPresent(System.out::println);
        // Optional<Object> o = Optional.ofNullable(null);
        // o.ifPresent(System.out::println);
        // Optional<Object> empty = Optional.empty();
        // if (empty.isPresent()) {
        //     System.out.println("不知道");
        // }
        Student student = new Student(1, "zhangsan", "M");
        
        Optional<Student> optional = Optional.ofNullable(student);
        Optional<String> s = optional.map(Student::getName);
        s.ifPresent(System.out::println);
        
        Optional<Student> student1 = Optional.ofNullable(new Student(1, "name", "M"));// try {
        Optional<String> s1 = student1.map(Student::getName);
        System.out.println(s1);
        Optional<String> u = student1.flatMap(student2 -> Optional.ofNullable(student2.getName()));
        
        //
        System.out.println("=====");
        Optional<Student> student2 = student1.filter(student22 -> false);
        System.out.println(student2);
        
        //
        Student student3 = student2.orElse(new Student(1, "aaa", "M"));
        System.out.println(student3);
        
        //

//     Student student1 = optional.orElseThrow(MyException::new);
        //     System.out.println(student1);
        // } catch (MyException e) {
        //     e.printStackTrace();
        // }
        // if (optional.isPresent()) {
        //     System.out.println("student");
        //     // System.out.println(optional.get());
        //    
        // } else {
        //     System.out.println("student不存在");
        // }
        
    }
}
